
//
//  HMEmotionListView.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMEmotionListView.h"
#import "HMEmotionGridView.h"



//表情的最大行数
#define HMEmotionMaxRows 3
//表情的最大列数
#define HMEmotionMaxCols 7
//每页表情的最大数量:最大行数*最大列数-1(因为每页的右下角都是删除按钮)
//[一般为3*7-1=20个]
#define HMEmotionMaxCountPerPage (HMEmotionMaxRows * HMEmotionMaxCols - 1)



@interface HMEmotionListView()<UIScrollViewDelegate>

//1.上部分:scrollview
@property (nonatomic, strong) UIScrollView *scrollView;
//2.下部分 pageControl
@property (nonatomic, strong) UIPageControl *pageControl;


@end


@implementation HMEmotionListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.添加 scrollView
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
//        HMLog(@"%@", scrollView.subviews);
        
        
        //2.添加 pageControl
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_keyboard_dot_normal"]];
        pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_keyboard_dot_selected"]];
        
        [self addSubview:pageControl];
        self.pageControl = pageControl;
        
    }
    return self;
}


//根据传进来的数据,计算一共有多少页
//重写 数组 的setter 方法
- (void)setEmotions:(NSArray *)emotions{
    //emotions 数组里 包含 所有的表情
    _emotions = emotions;
    
    //注意:每次调用 setEmotions 方法,都会向scrollView 中添加控件,需要先清空
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
//     HMLog(@"前:%ld", self.scrollView.subviews.count);
    //设置总页数
    self.pageControl.numberOfPages = (emotions.count + HMEmotionMaxCountPerPage - 1) / HMEmotionMaxCountPerPage;
    //点击 "默认" / "Emoji" / "浪小花",来回切换的时候,应当初始化当前页码为0
    self.pageControl.currentPage = 0;
    
    
    //决定scrollView 显示多少页表情(每一页都是一个HMEmotionGridView)
    for (int i = 0; i < self.pageControl.numberOfPages; i++) {
        
        //HMEmotionGridView 表示 一个展示表情的页面
        //emotions 数组里 包含 所有的表情,一个页面最多可展示20个,所以需要对所有表情进行分割
        
        int loc = i * HMEmotionMaxCountPerPage;
        int length = HMEmotionMaxCountPerPage;
        //注意对是否越界进行判断
        if (loc + length > emotions.count) {
            length = (int)emotions.count - loc;
        }
        
        NSRange gridViewRange= NSMakeRange(loc, length);
        NSArray *gridViewArray = [emotions subarrayWithRange:gridViewRange];
        HMEmotionGridView *gridView = [[HMEmotionGridView alloc] init];
//        gridView.backgroundColor = HMRandomColor;
        gridView.emotions = gridViewArray;
        [self.scrollView addSubview:gridView];
    }
//     HMLog(@"后:%ld", self.scrollView.subviews.count);
    
    
    //重新布局子控件
    [self setNeedsLayout];
    
    //点击 "默认" / "Emoji" / "浪小花",来回切换的时候,应当初始化当前scrollView 偏移量为0
    self.scrollView.contentOffset = CGPointZero;

}



//设置frame
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //1.设置 pageControl frame
    CGFloat pageControlW = self.width;
    CGFloat pageControlH = 35;
    CGFloat pageControlX = 0;
    CGFloat pageControlY = self.height - pageControlH;
    self.pageControl.frame = CGRectMake(pageControlX, pageControlY, pageControlW, pageControlH);
    
    //2.设置 scrollView frame
    CGFloat scrollViewW = self.width;
    CGFloat scrollViewH = pageControlY;
    CGFloat scrollViewX = 0;
    CGFloat scrollViewY = 0;
    self.scrollView.frame = CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH);
    
    
    //3.设置 UIScrollView 内部控件的尺寸
    int count = (int)self.pageControl.numberOfPages;
    CGFloat gridW = self.scrollView.width;
    CGFloat gridH = self.scrollView.height;
    self.scrollView.contentSize = CGSizeMake(count * gridW, 0);
    //遍历scrollView 子控件需要注意 里面究竟包含了哪些东西

    for (int i = 0; i < count; i++) {
        UIView *gridView = self.scrollView.subviews[i];
        gridView.frame = CGRectMake(i * gridW, 0, gridW, gridH);
        
    }
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.width + 0.5);
}

@end
